<?php

namespace App\Http\Requests\Seller\Goods;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
            'name' => ['required', 'bail'],
            'status' => ['required', 'boolean', 'bail'],
            'type_id' => ['required', 'exists:goods_types,id', 'bail'],
            'sku'=>['required','array','bail'],
            'sku.*.name' => ['required', 'bail',function($attr,$val,$fail){
            }],
            'sku.*.stock' => ['required', 'numeric', function ($attribute, $value, $fail) {
                if ($value < 0) {
                    $fail(':attribute是无效的');
                }
            }],
            'sku.*.price'=>['required','numeric',function($attribute,$value,$fail){
                if($value<=0){
                    $fail(':attribute不能小于0');
                }
            },'bail'],
            'sku.*.status'=>['required','boolean','bail']
        ];
    }

    public function attributes()
    {
        return [
            'name' => '商品名称',
            'status' => '商品状态',
            'type_id' => '商品类型',
            'sku.*.name' => 'sku名称',
            'sku.*.stock'=>'sku库存',
            'sku.*.price'=>'sku价格',
            'sku.*.status'=>'sku状态',
        ];
    }
    public function messages()
    {
        return [
            'sku.required'=>'sku至少有一条'
        ];
    }
}
